#include <assert.h>
#include <imbus.h>
#include <stdio.h>
#include <string.h>

void print_attribute (IMPointer attr, IMPointer user_data)
{
    static char *underline_names[] = { "None", "Single", "Double", "Wave" };
    static char *font_style_names [] = { "Normal", "Oblique", "Italic" };
    static char *font_weight_names [] = { "Normal", "Bold" };

    IMType type = IM_ATTRIBUTE_TYPE (attr);

    printf ("%16s %3d -%3d: ",
            im_type_get_name (IM_ATTRIBUTE_TYPE (attr)),
            im_attribute_get_start (attr),
            im_attribute_get_end (attr));

    if (IM_TYPE_ATTR_LANGUAGE == type)
        printf ("%s", im_attr_language_get (attr));
    else if (IM_TYPE_ATTR_FONT_FAMILY == type)
        printf ("%s", im_attr_font_family_get (attr));
    else if (IM_TYPE_ATTR_STRING == type)
        printf ("%s", im_attr_string_get (attr));
    else if (IM_TYPE_ATTR_FONT_STYLE == type)
        printf ("%s", font_style_names [im_attr_font_style_get (attr)]);
    else if (IM_TYPE_ATTR_FONT_WEIGHT == type)
        printf ("%s", font_weight_names [im_attr_font_weight_get (attr)]);
    else if (IM_TYPE_ATTR_FONT_SIZE == type)
        printf ("%d", im_attr_font_size_get (attr));
    else if (IM_TYPE_ATTR_UNDERLINE == type)
        printf ("%s", underline_names [im_attr_underline_get (attr)]);
    else if (IM_TYPE_ATTR_FOREGROUND == type) {
        IMUInt16 red, green, blue;
        im_attr_foreground_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_BACKGROUND == type) {
        IMUInt16 red, green, blue;
        im_attr_background_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_UNDERLINE_COLOR == type) {
        IMUInt16 red, green, blue;
        im_attr_underline_color_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_STRIKETHROUGH_COLOR == type) {
        IMUInt16 red, green, blue;
        im_attr_strikethrough_color_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue); 
    } else if (IM_TYPE_ATTR_OBJECT == type)
        printf ("%s\n", im_type_get_name (im_object_get_type (im_attr_object_get (attr))));

    printf ("\n");
}

void print_text(IMText *text)
{
    IMSize utf8len;
    IMChar *buf;
    IMAttrList *attrs;

    utf8len = im_text_utf8_length (text);

    buf = im_malloc (utf8len + 1);

    utf8len = im_text_to_utf8_string (text, buf, utf8len + 1);

    buf [utf8len] = 0;

    printf ("Text %d: %s\n", im_text_length (text), buf);
    im_free (buf);

    attrs = im_text_get_attr_list (text);

    if (attrs)
        im_attr_list_foreach (attrs, print_attribute, 0);
}

static const IMUInt16 utf16str [] = {0x4e00, 0x4e01, 0x3400, 0xd840, 0xdc21, 0x3007, 0x0040, 0xd869, 0xdea9, 0x002d, 0};
static const IMUniChar ucs4str [] = {0x4e00, 0x4e01, 0x3400, 0x20021, 0x3007, 0x0040, 0x2a6a9, 0x002d, 0};

int main (int argc, char *argv [])
{
    IMText *text1;
    IMText *text2;
    IMText *text3;

    im_init (&argc, &argv);

    text1 = im_text_new_from_utf8_string ("Hello world! It's an UTF-8 string, 大家好𠁎㐀一.", -1);
    text2 = im_text_new_from_utf16_string (utf16str, -1);
    text3 = im_text_new_from_ucs4_string (ucs4str, -1);

    im_text_add_attribute (text1, 0, 5, im_attr_underline_new (IM_ATTR_UNDERLINE_SINGLE));
    im_text_add_attribute (text1, 6, 5, im_attr_font_style_new (IM_ATTR_FONT_STYLE_ITALIC));
    im_text_add_attribute (text1, 13, 20, im_attr_strikethrough_new ());
    im_text_add_attribute (text1, 34, 10, im_attr_foreground_new (10, 20, 30));

    im_text_add_attribute (text2, 3, 1, im_attr_highlight_new ());
    im_text_add_attribute (text2, 6, 1, im_attr_highlight_new ());

    print_text (text1);
    print_text (text2);
    print_text (text3);

    if (im_text_compare (text1, text2) != 0)
        printf ("text1 and text2 differ.\n");
    if (im_text_compare (text2, text3) != 0)
        printf ("text2 and text3 differ.\n");

    im_text_insert (text1, 20, text2);
    print_text (text1);

    im_text_erase (text1, 24, 2);
    print_text (text1);

    im_text_insert_utf16_string (text1, 5, utf16str, -1);
    print_text (text1);

    im_text_prepend_utf8_printf (text1, "%s 12345 %s", "大家好", "Hmmmm.");
    print_text (text1);

    im_text_insert_utf8_printf (text1, 24, "另一个 %f", 3.14159);
    print_text (text1);

    im_text_append_utf8_printf (text1, "尾巴");
    print_text (text1);

    im_object_unref (text1);
    im_object_unref (text2);
    im_object_unref (text3);
    return 0;
}
/*
vi:ts=4:nowrap:ai:expandtab
*/
